import $service from '../../../../service.js';
var doctor_id;
var that;
var this_;
var hospital_id = "1"
var inquiryMap = {}
var template_id;
var numText; //添加文字用
Page({
    data: {
        tabactive: 1,
        templateList: [],
        mould: 1,
        text: '', //显示内容
        iinquiry_patient_allergy_details: '', //过敏史
        log_main_suit: '', //主诉
        log_present_illness: '', //现病史
        log_history_present_illness: '', //既往史
        lof_diagnosis: '', //诊断
        isdetalis: false,
        isdetalistype: 1,
        template_type: '', //模板类型
        template_name: '', //模板名称
        template_details: '', //模板详情
        template_list: ['主诉', '现病史', '既往史'],
        islist2: false,
        isdiagnosis: false, //自定义诊断弹窗
        isdiagnosisText: '', //自定义诊断
        name: '', //搜索
        diagnosis_list: [], //已开诊断列表
        activeIndex: '', //诊断列表选中下标
        inputtext: '' //补充的值
    },
    onLoad(e) { ////index 0过敏史 1 主诉 2现病史 3既往史 4诊断
        this_ = this
        that = this.data;
        inquiryMap = $service.strToJson(e.inquiryMap);
        if (e.index == 0) {
            this.data.text = inquiryMap.iinquiry_patient_allergy_details
            var title = '药物过敏史'
        } else if (e.index == 1) {
            var title = '填写主诉'
            this.data.text = inquiryMap.log_main_suit
        } else if (e.index == 2) {
            var title = '填写主诉'
            this.data.text = inquiryMap.log_present_illness
        } else if (e.index == 3) {
            var title = '填写主诉'
            this.data.text = inquiryMap.log_history_present_illness
        } else if (e.index == 4) {
            var title = '填写诊断'
            this.getlofdiagnosis(inquiryMap.lof_diagnosis, 1)
        }
        wx.setNavigationBarTitle({
            title: title
        })
        this.setData({
            tabactive: e.index,
            text: this.data.text,
            iinquiry_patient_allergy_details: inquiryMap.iinquiry_patient_allergy_details, //过敏史
            log_main_suit: inquiryMap.log_main_suit, //主诉
            log_present_illness: inquiryMap.log_present_illness, //现病史
            log_history_present_illness: inquiryMap.log_history_present_illness, //既往史
            lof_diagnosis: inquiryMap.lof_diagnosis //诊断
        })
    },
    onShow() {
        this_ = this
        that = this.data;
        doctor_id = $service.getUserLocalStorage().doctor_id;
        if (!$service.isUndefinedAndEmpty($service.getUserLocalStorage().hospital_id)) {
            hospital_id = $service.getUserLocalStorage().hospital_id
        }
        this.getdoctorSide_personal_inquiry()
    },
    getdoctorSide_personal_inquiry() { //index 0过敏史 1 主诉 2现病史 3既往史 4诊断
        if (that.tabactive == 0) {
            that.text = that.iinquiry_patient_allergy_details
        } else if (that.tabactive == 1) {
            that.text = that.log_main_suit
        } else if (that.tabactive == 2) {
            that.text = that.log_present_illness
        } else if (that.tabactive == 3) {
            that.text = that.log_history_present_illness
        }
        this.setData({
            text: that.text
        })
        if (that.tabactive == 1 || that.tabactive == 2 || that.tabactive == 3) {
            this.doctorSide_personal_inquiry_getDoctorTemplate()
        } else if (that.tabactive == 4) {
            this.doctorSide_personal_prescription_getDiagnosisList()
        }
    },
    //诊断遍历
    getlofdiagnosis(diagnosisText, num) {
        var nameList = [];
        var numC = 0

        if (num == 1) { //map
            if (!$service.isUndefinedAndEmpty(diagnosisText)) {
                var lofdiagnosislist = diagnosisText.split(",")
                for (var i = 0; i < lofdiagnosislist.length; i++) {
                    var index = lofdiagnosislist[i].indexOf('自定义诊断');
                    if (index > -1) {
                        var json = {
                            "name": lofdiagnosislist[i],
                            "type": "1",

                        }
                    } else {
                        numC++
                        var json = {
                            "name": lofdiagnosislist[i],
                            "type": "2",
                            "num": numC
                        }
                    }
                    nameList.push(json);
                }
            }
        } else if (num == 2) { //list
            for (var i = 0; i < diagnosisText.length; i++) {
                if (diagnosisText[i].type == "1") {
                    var json = {
                        "name": diagnosisText[i].name,
                        "type": "1",

                    }
                } else {
                    numC++
                    var json = {
                        "name": diagnosisText[i].name,
                        "type": "2",
                        "num": numC
                    }
                }
                nameList.push(json);
            }

        }
        this.setData({
            diagnosis_list: nameList,
        })


    },
    // 主诉 2现病史 3既往史 
    doctorSide_personal_inquiry_getDoctorTemplate: function() {
        if (that.mould == 1) {
            var doctorid = 0
        } else {
            var doctorid = doctor_id
        }
        var jsonData = {
            "doctor_id": doctorid,
            "template_type": that.tabactive, //1 主诉 2现病史 3既往史 
            "template_name": that.name
        }
        var jsonAjax = {
            "url": "doctorSide_personal_inquiry_getDoctorTemplate.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_inquiry_getDoctorTemplate_back);
    },
    doctorSide_personal_inquiry_getDoctorTemplate_back: function(jsonBack) {
        var data = $service.strToJson(jsonBack.data).data;
        var templateList = data.templateList; //列表
        this.setData({
            templateList: templateList
        })
    },
    // 诊断列表
    doctorSide_personal_prescription_getDiagnosisList: function() {
        var jsonData = {
            "hospital_id": hospital_id,
            "queryName": that.name
        }
        var jsonAjax = {
            "url": "doctorSide_personal_prescription_getDiagnosisList.do",
            "jsonData": jsonData,
        }
        $service.getAjaxData(jsonAjax, this.doctorSide_personal_prescription_getDiagnosisList_back);
    },
    doctorSide_personal_prescription_getDiagnosisList_back: function(jsonBack) {
        var data = $service.strToJson(jsonBack.data).data;
        var diagnosisList = data.diagnosisList; //列表
        this.setData({
            templateList: diagnosisList
        })
    },
    taptab(e) {
        var index = e.currentTarget.dataset.index;
        var num = e.currentTarget.dataset.num;
        if (num == 1) {
            that.tabactive = index
        } else if (num == 2) {
            that.mould = index
        }
        this.setData({
            mould: that.mould,
            tabactive: that.tabactive
        })
        this.getdoctorSide_personal_inquiry()
    },
    taptemplateList(e) {

        var index = e.currentTarget.dataset.index; //下标
        var num = e.currentTarget.dataset.num; //1删除模板 2编辑模板 3 引用模板
        if (num == 1) {
            wx.showModal({
                title: '温馨提示',
                content: '确定要删除当前常用模板？',
                success(res) {
                    if (res.confirm) {
                        var jsonData = {
                            "template_id": that.templateList[index].template_id
                        }
                        var jsonAjax = {
                            "url": "doctorSide_personal_prescription_deleteUtilDiagnosis.do",
                            "jsonData": jsonData,
                        }
                        $service.getAjaxData(jsonAjax, function(jsonObj) {
                            $service.showToast('删除成功')
                            this_.getdoctorSide_personal_inquiry()
                        });
                    }
                }
            })
        } else if (num == 2) { //编辑模板
            template_id = that.templateList[index].template_id
            this.setData({
                isdetalis: true,
                isdetalistype: 2,
                template_type: '', //模板类型
                template_name: '', //模板名称
                template_details: '' //模板内容
            })
        } else if (num == 3) {
            if(!$service.isUndefinedAndEmpty(that.text)){
                if (that.text.indexOf(that.templateList[index].template_details) > -1) {
                    $service.showToast('请勿重复添加');
                    return;
                }
                var valtext = that.text + '、' + that.templateList[index].template_details;
            }else {
                var valtext = that.templateList[index].template_details;
            }
            if (that.tabactive == 1) {
                that.log_main_suit = valtext
            } else if (that.tabactive == 2) {
                that.log_present_illness = valtext
            } else if (that.tabactive == 3) {
                that.log_history_present_illness = valtext
            } else if (that.tabactive == 4) {
                that.lof_diagnosis = valtext
            }
            this.setData({
                iinquiry_patient_allergy_details: that.iinquiry_patient_allergy_details, //过敏史
                log_main_suit: that.log_main_suit, //主诉
                log_present_illness: that.log_present_illness, //现病史
                log_history_present_illness: that.log_history_present_illness, //既往史
                lof_diagnosis: that.lof_diagnosis //诊断
            })
            this.getdoctorSide_personal_inquiry()
        }
    },
    btn(e) {
        var index = e.currentTarget.dataset.index; //1清空 2保存模板() 3保存
        if (index == 1) {
            if (that.tabactive == 0) {
                that.iinquiry_patient_allergy_details = ''
            } else if (that.tabactive == 1) {
                that.log_main_suit = ''
            } else if (that.tabactive == 2) {
                that.log_present_illness = ''
            } else if (that.tabactive == 3) {
                that.log_history_present_illness = ''
            } else if (that.tabactive == 4) {
                that.diagnosis_list = []
            }
            this.setData({
                iinquiry_patient_allergy_details: that.iinquiry_patient_allergy_details, //过敏史
                log_main_suit: that.log_main_suit, //主诉
                log_present_illness: that.log_present_illness, //现病史
                log_history_present_illness: that.log_history_present_illness, //既往史
                diagnosis_list: that.diagnosis_list //诊断
            })
            this.getdoctorSide_personal_inquiry()
        } else if (index == 2) { //保存模板
            this.setData({
                isdetalis: true,
                isdetalistype: 1,
                template_type: '', //模板类型
                template_name: '', //模板名称
                template_details: '' //模板内容
            })
        } else if (index == 3) { //保存
            if (that.tabactive == 0) { //药物过敏史
                var json = { //
                    "log_id": inquiryMap.log_id,
                    "iinquiry_patient_allergy_details": that.text,
                    "lof_diagnosis": inquiryMap.lof_diagnosis,
                    "type": '1'
                }

            } else if (that.tabactive == 1 || that.tabactive == 2 || that.tabactive == 3) { //主诉、现病史、既往史 
                var json = { //主诉
                    "log_id": inquiryMap.log_id,
                    "log_main_suit": that.log_main_suit,
                    "log_present_illness": that.log_present_illness,
                    "log_history_present_illness": that.log_history_present_illness,
                    "lof_diagnosis": inquiryMap.lof_diagnosis,
                    "type": '2'
                }
            } else if (that.tabactive == 4) { //诊断
                var txt = '';
                for (var i = 0; i < that.diagnosis_list.length; i++) {
                    txt += that.diagnosis_list[i].name + ","
                }
                txt = txt.substring(0, txt.length - 1);
                if ($service.isUndefinedAndEmpty(txt)) {
                    txt = "";
                }
                var json = {
                    "log_id": inquiryMap.log_id,
                    "lof_diagnosis": txt,
                    "log_main_suit": inquiryMap.log_main_suit,
                    "log_present_illness": inquiryMap.log_present_illness,
                    "log_history_present_illness": inquiryMap.log_history_present_illness,
                    "type": '3'
                }
            }
            var jsonAjax = {
                "url": 'doctorSide_personal_inquiry_updateInquiry.do',
                "jsonData": json,
            }
            $service.getAjaxData(jsonAjax, function(jsonBack) {
                var returndata=$service.strToJson(jsonBack.data).data;
                var selToID='';
                if(inquiryMap.join_type == 1){
                     selToID = "U" + inquiryMap.join_id
                }else{
                      selToID = "S" + inquiryMap.join_id
                }
                if (that.tabactive == 0) {
                     var sendSystemMessageMsgText = '医生修改了您的过敏史'
                }
                if("1"==returndata.diagnosisSuit){
                    var sendSystemMessageMsgText = '医生已下达诊断'
                }else if("2"==returndata.diagnosisSuit){
                    var sendSystemMessageMsgText = '医生已修改诊断'
                }
                if("1"==returndata.mainSuit){
                    var sendSystemMessageMsgText = '医生已录入主诉'
                }else if("2"==returndata.mainSuit){
                    var sendSystemMessageMsgText = '医生已修改主诉'
                }
                
                if("1"==returndata.presentIllnessSuit){
                    var sendSystemMessageMsgText = '医生已录入现病史'
                }else if("2"==returndata.presentIllnessSuit){
                    var sendSystemMessageMsgText = '医生已修改现病史'
                }
        
                if("1"==returndata.historyPresentIllnessSuit){
                    var sendSystemMessageMsgText = '医生已录入既往史'
                }else if("2"==returndata.historyPresentIllnessSuit){
                    var sendSystemMessageMsgText = '医生已修改既往史'
                }
                wx.txTic.sendCustomMessage(selToID,7)
                wx.txTic.sendSystemMessageMsg(selToID,sendSystemMessageMsgText);
                $service.showToast('保存成功')
                wx.navigateBack({
                    delta: 1
                })
            });
        } else if (index == 4) { //取消生成个人模板

            this.setData({
                isdetalis: false,
                isdiagnosis: false,
                isdiagnosisText: that.isdiagnosisText

            })
        } else if (index == 5) { //确认生成个人模板

            if ($service.isUndefinedAndEmpty(that.template_type)) {
                $service.showToast('请选择模板类型');
                return;
            }
            if ($service.isUndefinedAndEmpty(that.template_name)) {
                $service.showToast('请输入模板名称');
                return;
            }
            if ($service.isUndefinedAndEmpty(that.template_details)) {
                $service.showToast('请输入模板内容');
                return;
            }
            if (that.template_type == "主诉") {
                var template_type = "1"
            } else if (that.template_type == "现病史") {
                var template_type = "2"
            } else if (that.template_type == "既往史") {
                var template_type = "3"
            }
            if (that.isdetalistype == 1) {
                var jsonData = {
                    "doctor_id": doctor_id,
                    "hospital_id": hospital_id,
                    "template_type": template_type,
                    "template_name": that.template_name,
                    "template_details": that.template_details
                }
                var url = 'doctorSide_personal_inquiry_addDoctorTemplate.do'
            } else {
                var jsonData = {
                    "template_id": template_id,
                    "template_type": template_type,
                    "template_name": that.template_name,
                    "template_details": that.template_details
                }
                var url = 'doctorSide_personal_prescription_updateDoctorTemplate.do'
            }
            var jsonAjax = {
                "url": url,
                "jsonData": jsonData,
            }
            $service.getAjaxData(jsonAjax, function(jsonObj) {
                $service.showToast('保存成功')
                this_.setData({
                    template_type: '', //模板类型
                    template_name: '', //模板名称
                    template_details: '', //模板内容
                    isdetalis: false
                })
                this_.getdoctorSide_personal_inquiry()
            });
        } else if (index == 6) { //打开自定义诊断
            for (var i = 0; i < that.diagnosis_list.length; i++) {
                if (that.diagnosis_list[i].type == "1") {
                    that.isdiagnosisText = that.diagnosis_list[i].name.substring(6, 1000)
                }
            }
            this.setData({
                isdiagnosis: true,
                isdiagnosisText: that.isdiagnosisText
            })
        } else if (index == 7) { //保存诊断
            var num = 0
            for (var i = 0; i < that.diagnosis_list.length; i++) {
                if (that.diagnosis_list[i].type == "1") {
                    num = 1
                    that.diagnosis_list[i].name = '自定义诊断：' + that.isdiagnosisText
                }
            }
            if (num == 0) {
                var json = {
                    "name": '自定义诊断：' + that.isdiagnosisText,
                    "type": "1",

                }
                that.diagnosis_list.push(json)
            }
            this.setData({
                isdiagnosis: false,
                diagnosis_list: that.diagnosis_list
            })
        }
    },
    tap_usage(e) {
        var num = e.currentTarget.dataset.num; //
        if (num == 1) {
            that.islist2 = !that.islist2
        } else {
            var index = e.currentTarget.dataset.index; //0主诉 1现病史 2既往史
            that.template_type = index
            that.islist2 = false

        }
        this.setData({
            template_type: that.template_type,
            islist2: that.islist2
        })
    },
    // 输入框监听
    inputChange(e) {
        var value = e.detail.value;
        var index = e.currentTarget.dataset.index; //
        if (index == 1) {
            if (that.tabactive == 0) {
                that.iinquiry_patient_allergy_details = value
            } else if (that.tabactive == 1) {
                that.log_main_suit = value
            } else if (that.tabactive == 2) {
                that.log_present_illness = value
            } else if (that.tabactive == 3) {
                that.log_history_present_illness = value
            } else if (that.tabactive == 4) {
                that.lof_diagnosis = value
            }
        } else if (index == 2) {
            that.template_name = value
        } else if (index == 3) {
            that.template_details = value
        } else if (index == 4) {
            that.name = value
        } else if (index == 5) {
            that.inputtext = ''
            for (var i = 0; i < that.diagnosis_list.length; i++) {
                if (that.diagnosis_list[i].name == numText) {
                    that.diagnosis_list[i].name = that.diagnosis_list[i].name + value
                }
            }
        } else if (index == 6) {
            that.isdiagnosisText = value
        }
        this.setData({
            iinquiry_patient_allergy_details: that.iinquiry_patient_allergy_details, //过敏史
            log_main_suit: that.log_main_suit, //主诉
            log_present_illness: that.log_present_illness, //现病史
            log_history_present_illness: that.log_history_present_illness, //既往史
            lof_diagnosis: that.lof_diagnosis, //诊断
            template_name: that.template_name, //模板名称
            template_details: that.template_details, //模板内容
            name: that.name,
            inputtext: that.inputtext,
            diagnosis_list: that.diagnosis_list,
            activeIndex: '',
            isdiagnosisText: that.isdiagnosisText
        })
        this.getdoctorSide_personal_inquiry()
    },
    //删除诊断列表
    delImg(e) {
        var index = e.currentTarget.dataset.index; //
        var list = that.diagnosis_list
        for (var i = 0; i < that.diagnosis_list.length; i++) {
            if (index == i) {
                if (that.diagnosis_list[i].type == "1") {
                    that.isdiagnosisText = ''
                }
                list.splice(i, 1)
            }
        }
        this.setData({
            isdiagnosisText: that.isdiagnosisText
        })
        this.getlofdiagnosis(list, 2)
    },
    //选中诊断列表
    templateListtap(e) {
        var index = e.currentTarget.dataset.index; //
        var num = 0;
        for (var i = 0; i < that.diagnosis_list.length; i++) {
            if (that.diagnosis_list[i].type == 2) {
                num++
            }
            if (that.diagnosis_list[i].name == that.templateList[index].diagnosis_name) {
                $service.showToast('有重复诊断!');
                return
            }
        }
        if (num > 2) {
            $service.showToast('最多只能添加三个');
            return
        } else {
            var json = {
                "name": that.templateList[index].diagnosis_name,
                "type": "2",
                "num": (num + 1)
            }
            that.diagnosis_list.push(json)
            numText = json.name
        }
        this.setData({
            activeIndex: index, //
            diagnosis_list: that.diagnosis_list
        })

    }
})
